package com.java.softproblem.exer3;

/**
 * @author luzhikun
 * @date &{DATE}-23:04
 */
public class SyscTest1 {
    public static void main(String[] args) {
        MyThread_1 myThread = new MyThread_1("线程一");
        MyThread_1 myThread_1 = new MyThread_1("线程二");
        MyThread_1 myThread_11 = new MyThread_1("线程三");
        myThread.start();
        myThread_1.start();
        myThread_11.start();

    }
}

class MyThread_1 extends Thread {
    private static int ticket = 100;
    private static Object object = new Object();

    public MyThread_1(String name) {
        super(name);
    }

    public void run() {
        while (true) {
            //正确的
            synchronized (object) {
                //错误的
//            synchronized (this) {  此时的this代表着myThread\myThread_1\myThread_11
                if (ticket > 0) {
                    System.out.println(getName() + "恭喜你买到了票" + ticket);
                    ticket--;
                } else {
                    System.out.println("对不起没有票了");
                    break;
                }
            }
        }
    }

}
